package com.lagou.mapper;

import com.lagou.domain.Role;
import com.lagou.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserAnno {
	@Select("select * from user")
	public List<User> findAll();

	@Delete("delete from user where id = #{id}")
	public void deleteById(Integer id);

	@Update("update user set username=#{username},address = #{address} where id=#{id}")
	public void update(User user);

	@Insert("insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})")
	public void insert(User user);

	@Select("select * from user where id = #{id}")
	public User selectById(Integer id);


	@Select("select * from user")
	@Results({
		@Result(property = "id",column = "id",id = true),
		@Result(property = "username",column = "username"),
		@Result(property = "birthday",column = "birthday"),
		@Result(property = "sex",column = "sex"),
		@Result(property = "address",column = "address"),
		@Result(property = "orders",javaType = List.class,column = "id",many = @Many(select="com.lagou.mapper.OrderAnno.findOrdersByUserId"))
	})
	public List<User> findAllWithOrder();

	@Select("select * from user")
	@Results({
			@Result(property = "id",column = "id",id = true),
			@Result(property = "username",column = "username"),
			@Result(property = "birthday",column = "birthday"),
			@Result(property = "sex",column = "sex"),
			@Result(property = "address",column = "address"),
			@Result(property = "roles",column = "id",javaType = List.class,
					many = @Many(select="com.lagou.mapper.RoleAnno.findRoleByUid",fetchType = FetchType.EAGER))
	})
	public List<User> findAllWithRole();

}
